package com.azdebugit.java8.suanfa.mingan;

import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @date 2021/9/29 17:46
 * <p>
 * 抽象违禁词文件加载器
 */
@SuppressWarnings({"unused", "WeakerAccess"})
public abstract class SensitiveWordAbstractFileLoader implements SensitiveWordLoader {

    /**
     * 违禁词文件路径
     */
    protected String filePath;

    /**
     * 文件字符集
     */
    protected Charset charset;

    /**
     * 关键词分隔符
     */
    protected String separator;

    protected InputStream inputStream;

    /**
     * 加载标识
     */
    private static final AtomicBoolean LOAD_FLAG = new AtomicBoolean(false);

    /**
     * 违禁词列表
     */
    private static List<String> sensitiveWordList;

    public SensitiveWordAbstractFileLoader(String filePath, Charset charset, String separator) {
        this.filePath = filePath;
        this.charset = charset;
        this.separator = separator;
        sensitiveWordList = new ArrayList<>();
        initInputStream();
    }

    /**
     * 初始化inputStream
     */
    public abstract void initInputStream();

    @Override
    @NonNull
    public List<String> load() {
        if (LOAD_FLAG.getAndSet(true)) {
            return sensitiveWordList;
        }
        InputStreamReader reader = null;
        BufferedReader bufferedReader = null;
        try {
            reader = new InputStreamReader(inputStream, charset.name());
            bufferedReader = new BufferedReader(reader);

            String singleLine;
            while ((singleLine = bufferedReader.readLine()) != null) {
//                sensitiveWordList.addAll((singleLine.trim().));
            }
            return sensitiveWordList;
        } catch (Exception e) {
            e.printStackTrace();
            LOAD_FLAG.set(false);
            throw new IllegalStateException("load sensitive word file failed,msg =" + e.getMessage());
        } finally {
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(reader);
            IOUtils.closeQuietly(bufferedReader);
        }
    }

    /**
     * 从文件单行内容中获取违禁词
     *
     * @param singleLine 单行内容
     * @return 提取到的违禁词列表
     */
//@NonNull
    private List<String> getList(String singleLine) {
        String[] array = singleLine.split(separator);
        List<String> list = new ArrayList<>();
        for (String str : array) {
            if (StringUtils.isNotBlank(str)) {
                list.add(str);
            }
        }
        return list;
    }

    public String getFilePath() {
        return filePath;
    }

    public Charset getCharset() {
        return charset;
    }

    public String getSeparator() {
        return separator;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public static List<String> getSensitiveWordList() {
        return sensitiveWordList;
    }
}